package com.kuaimacode.kframework.admin.controller.address;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.address.AddressService;
import com.kuaimacode.kframework.admin.services.user.UserService;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.user.Address;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.Subject;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author anzy
 * @date 2019/03/29
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/address"})
public class AddressController {
	private static String PAGE_FILE_NAME = "address/";

	@Autowired
	private AddressService addressServices;
	@Autowired
	private UserService userService;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						Address address,ModelMap model) {
		PageInfo<Address> pageInfo = addressServices.findAll(pageNum, pageSize,address);
		// pageInfo.list address
		model.put("pageInfo", pageInfo);
		model.put("search",address);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		Address address = new Address();
		map.put("address", address);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		Address address = addressServices.getAddressById(id);
		map.put("address", address);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(Address address, Boolean isAdd,ModelMap map) {
		try {
			if(!Optional.ofNullable(userService.selectByName(address.getUserName())).isPresent())
				return JsonResult.failure("用户不存在请检查");

			//查询用户是否存在
			Optional.ofNullable(userService.selectByName(address.getUserName())).ifPresent(user -> address.setUserId(user.getId()));

			if(isAdd){
				address.setCreateAt(new Date());
				address.setDeleteState(Constant.DeleteStateEnum.NORMAL.getCode().shortValue());
				// 新建保存
				addressServices.save(address);
			}else{
				// 编辑保存
				address.setUpdateAt(new Date());
				addressServices.updateById(address);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		Address address = addressServices.getById(id);
		map.put("address", address);
		return PAGE_FILE_NAME+"detail";
	}

	@RequestMapping(value = {"/delete/{id}"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			Address address = addressServices.getById(id);

			if(address == null){
				return JsonResult.failure("该地址不存在");
			}
			address.setDeleteState(Constant.DeleteStateEnum.DELETE.getCode().shortValue());
			addressServices.updateById(address);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
